package com.jx.gocomtask.webservice.service.impl;

import com.jx.gocomtask.webservice.DTO.MessageReturn;
import com.jx.gocomtask.webservice.DTO.StatusMessage;
import com.jx.gocomtask.webservice.DTO.TaskScope;
import com.jx.gocomtask.webservice.DTO.TaskStatus;
import com.jx.gocomtask.webservice.entity.ExtTaskMessage;
import com.jx.gocomtask.webservice.entity.ExtTaskUser;
import com.jx.gocomtask.webservice.service.GroupService;
import com.jx.gocomtask.webservice.service.SendMessageService;
import com.jx.gocomtask.webservice.service.SystemSuperApp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by LWD on 2017/8/11.
 */
@Service
public class SendMessageServiceImpl implements SendMessageService {

    @Autowired
    RestTemplate restTemplate;
    @Resource
    GroupService groupService;
    @Autowired
    SystemSuperApp systemSuperApp;

    @Override
    public MessageReturn sendMessage(String url, StatusMessage message) {
        MessageReturn messageReturn = new MessageReturn();
        int i = 0;
        try {
            messageReturn =  restTemplate.postForObject(url, message, MessageReturn.class);
        }catch (Exception e){
            i++;
            if(i > 3){
                messageReturn.setId("0");
                return messageReturn;
            }
            this.sendMessage(url, message);
        }
        return messageReturn;
    }

    @Override
    public StatusMessage updateStatusMessage(ExtTaskMessage extTaskMessage, String tag, String touserName) {
        StatusMessage statusMessage=new StatusMessage();
        StatusMessage.Begin b=new StatusMessage.Begin();

        b.setUrl(extTaskMessage.getUrl());
        b.setIos(extTaskMessage.getIos());
        b.setAndroid(extTaskMessage.getAndroid());
        if(touserName!=null){
            StatusMessage.Content contentext=new StatusMessage.Content();
            b.setContent(contentext);
            List<StatusMessage.Form> forms=new ArrayList<>();

            switch (tag) {
                case "person":
                    contentext.setTitle(extTaskMessage.getExtTaskUser().get(0).getStatus().getDescTittle());
                    if (!extTaskMessage.getStatus().equals(TaskStatus.RECEIVED))
                        contentext.setTitle(extTaskMessage.getStatus().getDescTittle());

                    break;
                case "create":
                    contentext.setTitle(extTaskMessage.getStatus().getDescTittle());
                    break;
            }

            contentext.setDetail(extTaskMessage.getContent());

            forms.add(new StatusMessage.Form("发起人", extTaskMessage.getFromUserTrueName()));
            forms.add(new StatusMessage.Form("接收人", touserName));
            forms.add(new StatusMessage.Form("来  源", groupService.findByGuId(extTaskMessage.getGroup()).getGroupname()));
            contentext.setForm(forms);
            statusMessage.setBegin(b);
        }else{
            statusMessage.setMiddle(b);
            extTaskMessage.setOld(true);
        }//preUpadeStatusExtTaskMessage
        switch (tag){
            case "person":
                statusMessage.setTouser(extTaskMessage.getExtTaskUser().get(0));
                statusMessage.setTogroup(null);
                b.setId(extTaskMessage.getGocomCMsgId());
                break;
            case "create":
                statusMessage.setTouser(extTaskMessage.getFromUser());
                statusMessage.setTogroup(null);
                b.setId(extTaskMessage.getGocomCMsgId());
                break;
//			todo 减少群内
//			case "group":
//				statusMessage.setTogroup(extTaskMessage.getGroup());
//				statusMessage.setTouser(null);
//				b.setId(extTaskMessage.getGocomGMsgId());
//				break;
        }
        return statusMessage;
    }

    //    获取发送url
    public String  sendUrl(String gocomeApiUrl, String appid,String method){
        return gocomeApiUrl +method+systemSuperApp.appid_token(appid);
    }
}
